package mvc.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.util.Observable;
import java.util.Observer;
import java.util.ResourceBundle.Control;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import mvc.controller.Controller;
import mvc.model.Point;


/*
 *  This is a small example to show you how to develop a nice
 *  MVC structure using the observer-observable pattern
 *  Read http://www.javaworld.com/javaworld/jw-10-1996/jw-10-howto.html 
 *  http://docs.oracle.com/javase/6/docs/api/java/util/Observable.html , and 
 *  http://docs.oracle.com/javase/6/docs/api/java/util/Observer.html
 *  for details.
 *  Make sure to check the changed point class as well.
 */
public class MenuGUI extends JPanel implements Observer{

	private Point point;
	private Controller control; 
	private double minX=0, minY=0; 
	private JMenuItem zoomMenuItem,resetMenuItem; 
	
	public MenuGUI() {
		super(true);	
		createGUI(); 
	}

	private void createGUI() {
		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("Main");
		zoomMenuItem = new JMenuItem("Zoom");
		resetMenuItem = new JMenuItem("Reset");
		
		point = new Point();
		
		// Create a controller instance
		control = new Controller();
		control.addObserver(this);
		point.addObserver(this);
		
		zoomMenuItem.addActionListener(control);
		resetMenuItem.addActionListener(control);
		
		menuBar.add(menu);

		menu.add(zoomMenuItem);
		menu.add(resetMenuItem);
	
		JFrame frame = new JFrame("Scale a Triangle");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setJMenuBar(menuBar);
		frame.add(this);
		frame.setPreferredSize(new Dimension(500,500));	
		frame.pack();
		frame.setVisible(true);
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.scale(control.getScaleFactor(),control.getScaleFactor());
        g2d.translate(minX,minY); // this will work for the scale but not for rotation.
        
        Polygon poly = new Polygon();
        poly.addPoint(140, 100);
        poly.addPoint(300, 300);
        poly.addPoint(100, 200);

        Rectangle2D bb = poly.getBounds2D();
        minX = -bb.getMinX()+20;
        minY = -bb.getMinY()+20;
        g2d.drawPolygon(poly);
        g2d.setColor(Color.red);
        g2d.draw(bb);
        
        g2d.setColor(new Color((float)Math.random(), 
        		(float) Math.random(), (float)Math.random()));
        g2d.fillOval((int)point.getX(), (int)point.getY(), 24, 24);
	}
	
	public static void main(String[] args) throws InterruptedException {
		MenuGUI gui = new MenuGUI();
		while(true){
			gui.point.setX(Math.random()*500);
			gui.point.setY(Math.random()*500);
			Thread.sleep(30);
		}
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		repaint();
	}
}
